package demo;

import java.util.ArrayList;
import java.util.Random;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: dings
 * Date: 2024-06-26
 * Time: 16:14
 */
public class Test {
    public static void buyCard(ArrayList<Card> listCard) {
        //买52张牌
        String[] suit = {"♥","♠","♦","♣"};//红桃、黑桃、方片、梅花
        for (int i = 1; i <= 13; i++) {
            for (int j = 0; j < 4; j++) {
                listCard.add(new Card(suit[j],i ));
            }
        }
    }
    public static void shuffleCard(ArrayList<Card> listCard) {
        //洗牌
        int size = listCard.size();//52
        Random ran = new Random();
        for (int i = size - 1; i > 0; i--) {
            int random = ran.nextInt(i);//生成的随机数范围为：[0,i)
            //交换位置
            Card card = listCard.get(i);
            listCard.set(i,listCard.get(random));
            listCard.set(random,card);
        }
    }
    public static void main(String[] args) {
        ArrayList<Card> listCard = new ArrayList<>();
        //生成52张牌
        buyCard(listCard);
        System.out.println("生成的52张有序牌："+listCard);
        //洗牌
        shuffleCard(listCard);
        System.out.println("打乱后的牌："+listCard);
        //摸牌(3人轮流摸牌 摸5轮)
        ArrayList<ArrayList<Card>> listPeo = new ArrayList<>();
        listPeo.add(new ArrayList<>());
        listPeo.add(new ArrayList<>());
        listPeo.add(new ArrayList<>());
        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 3; j++) {
                Card card = listCard.remove(0);
                listPeo.get(j).add(card);
            }
        }
        System.out.println("===摸牌===");
        for (int i = 0; i < 3; i++) {
            System.out.println("第"+(i+1)+"个人的牌："+listPeo.get(i));
        }
        System.out.println("===剩余的牌===");
        System.out.println(listCard);
    }
}
